<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="lib/bootstrap.min.css">
    <style>
    *{
        margin: 0;
        padding: 0;
    }
    body{
        background: url(img/1.jpg) no-repeat;
    }
    #box{
        width: 400px;
        height: 260px;
        border: 1px solid black;
        border-radius: 15px;
        position: fixed;
        left: 50%;
        top: 50%;
        transform: translate(-50%,-50%);
        /* background-color:rgba(201, 201, 209, 0.925); */
    }
    #box h3{
        position: fixed;
        left: 50%;
        transform: translateX(-50%);
        top: 40px;
    }
    #usermessage{
        /* border: 1px solid black; */
        /* width: 300px; */
        margin: 100px 0 0 50px;
    }
    #usermessage #username{
        margin-left: 5px;
    }
    #usermessage #password{
        margin-left: 7px;
    }
    #usermessage input{
        width: 200px;
        border: none;
        outline: none;
    }
    #usermessage p{
        margin-bottom: 10px;
    }
    #button{
        position: fixed;
        left: 50%;
        transform: translateX(-50%);
        width: 160px;
        display: flex;
        justify-content: space-between;
        margin-top: 10px;
        
    }
    #login,#register{
        background-color: royalblue;
        width: 50px;
        height: 40px;
        border: none;
        border-radius: 10px;
        outline: none;
        cursor: pointer;
    }
    #check{
        /* margin-right: 10px; */
        margin-top: 67px;
        margin-left: 45px;
        
    }
    </style>
</head>
<body>
    <div id="box">
        <h3>欢迎登录</h3>
        <div id="usermessage">
        <p>
           <span>用户名:</span>
            <input type="text" name="" id="username" placeholder="请输入用户名">
        </p>
        <p>
            <span>密&nbsp;&nbsp;&nbsp;码:</span>
            <input type="password" name="" id="password" placeholder="请输入密码">
        </p>
        </div>
        <div id="button">
            <button class="btn btn-primary" id="login" type="submit">登录</button>
            <button class="btn btn-primary" id="register" type="submit">注册</button>
        </div>
        <div id="check">
             <input type="checkbox" name="" id="checkbox">
             <span> 30天内免登录</span>
        </div>
    </div>

    <script src="lib/jquery.min.js"></script>
   <script src="lib/common.js"></script>

    <script>
    // 登录
    $("#login").click(function(){
        // alert(1)
        if($("#username").val() && $("#password").val()){
            $.ajax({
                url:`${address}/login`,
                method:"post",
                data:{
                    username:$("#username").val(),
                    password:$("#password").val()
                },
                success(data){
                    if(data==="error"){
                        alert("用户名或密码错误")
                    }
                   else{  

                    //  选中30天内免登录，获取当前时间 设置过期时间,cookie是永久存储，要设置过期时间
                    // 应为复选框的checked属性是原生js属性，所以要转节点
                   
                    if($("#checkbox")[0].checked){
                                let date = new Date();
                                date.setDate(date.getDate() + 30);
                                document.cookie = `message=${JSON.stringify(data)};expires=${date}`
                            }
                            sessionStorage.setItem("message",JSON.stringify(data))
                            location.href = `${address}/studentlist.html`
                    }
                }
            })
        }
    })
    </script>
</body>
</html>